﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sector7.Restaurant.EntityType;
using Sector7.Framework.Core;
using System.Data.SqlClient;
using Sector7.Framework.DB;

namespace Sector7.Restaurant.DAO.OleDB
{
    /// <summary>
    /// 费用信息数据访问对象
    /// </summary>
    public class ChargeDAO : DAOBase<RS_Charge>, IChargeDAO
    {
        public override object Create(RS_Charge entity)
        {
            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                    INSERT INTO RS_Charge
                            (ChargeId
                            ,ChargeItem
                            ,ChargeType
                            ,Amount
                            ,OccurTime
                            ,BillId
                            ,Remarks)
                        VALUES
                            (@ChargeId
                            ,@ChargeItem
                            ,@ChargeType
                            ,@Amount
                            ,@OccurTime
                            ,@BillId
                            ,@Remarks)";

                return opt.ExecuteNonQuery(sql,
                    DBUtil.CreateSqlParameter(entity.ChargeId, "ChargeId"),
                    DBUtil.CreateSqlParameter(entity.ChargeItem, "ChargeItem"),
                    DBUtil.CreateSqlParameter(entity.ChargeType, "ChargeType"),
                    DBUtil.CreateSqlParameter(entity.Amount, "Amount"),
                    DBUtil.CreateSqlParameter(entity.OccurTime, "OccurTime"),
                    DBUtil.CreateSqlParameter(entity.BillId, "BillId"),
                    DBUtil.CreateSqlParameter(entity.Remarks, "Remarks"));
            }
        }

        public override RS_Charge Read(object primaryKey)
        {
            throw new NotImplementedException();
        }

        public override List<RS_Charge> ReadAll()
        {
            throw new NotImplementedException();
        }

        public List<RS_Charge> ReadAll(DateTime begin, DateTime end)
        {
            List<RS_Charge> charges = new List<RS_Charge>();

            using (DBHelper opt = new DBHelper())
            {
                string sql = @"
                    SELECT *
                        FROM RS_Charge
                        WHERE
                            OccurTime >= @BeginTime AND OccurTime < @EndTime
                        ORDER BY OccurTime ASC";

                SqlDataReader reader = opt.ExecuteQuery(sql,
                    DBUtil.CreateSqlParameter(begin, "BeginTime"),
                    DBUtil.CreateSqlParameter(end, "EndTime"));

                while (reader.Read())
                {
                    object charge = new RS_Charge();
                    DBUtil.ReadData(reader, ref charge);

                    charges.Add(charge as RS_Charge);
                }
            }

            return charges;
        }
    }
}
